﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using blackhouse.Payments.Data;

namespace blackhouse.Payments {
    public class PaymentTransactionStatus {

        #region Fields

        private IPaymentsDataAccess dataAccess;

        #endregion

        #region Properties

        public int StatusId { get; private set; }
        public string Description { get; private set; }
        public bool Exist { get; private set; }

        #endregion

        #region Constructors

        public PaymentTransactionStatus(int statusId) {
            this.dataAccess = PaymentsDataFactory.Instance().GetPaymentDataAccess();
            PaymentTransactionStatusData data = this.dataAccess.GetPaymentTransactionStatus(statusId);
            if (data.StatusId == statusId) {
                this.SetInstance(data);
            }
        }

        #endregion

        #region Methods

        private void SetInstance(PaymentTransactionStatusData data) {
            this.StatusId = data.StatusId;
            this.Description = data.Description;
            this.Exist = true;
        }

        #endregion

        #region Predefined Payment Status

        private static PaymentTransactionStatus newTransaction;
        private static PaymentTransactionStatus cancelTransaction;
        private static PaymentTransactionStatus rejectedTransaction;
        private static PaymentTransactionStatus beginedTransaction;
        private static PaymentTransactionStatus waitingTransaction;
        private static PaymentTransactionStatus rejectedTransactionWaitingPayback;
        private static PaymentTransactionStatus completeTransaction;
        private static PaymentTransactionStatus invalidStatus;

        public static PaymentTransactionStatus NewTransaction {
            get {
                if (newTransaction == null)
                    newTransaction = new PaymentTransactionStatus(1);
                return newTransaction;
            }
        }

        public static PaymentTransactionStatus CancelTransaction {
            get {
                if (cancelTransaction == null)
                    cancelTransaction = new PaymentTransactionStatus(2);
                return cancelTransaction;
            }
        }

        public static PaymentTransactionStatus RejectedTransaction {
            get {
                if (rejectedTransaction == null)
                    rejectedTransaction = new PaymentTransactionStatus(3);
                return rejectedTransaction;
            }
        }

        public static PaymentTransactionStatus BeginedTransaction {
            get {
                if (beginedTransaction == null)
                    beginedTransaction = new PaymentTransactionStatus(4);
                return beginedTransaction;
            }
        }

        public static PaymentTransactionStatus WaitingTransaction {
            get {
                if (waitingTransaction == null)
                    waitingTransaction = new PaymentTransactionStatus(5);
                return waitingTransaction;
            }
        }

        public static PaymentTransactionStatus RejectedTransactionWaitingPayback {
            get {
                if (rejectedTransactionWaitingPayback == null)
                    rejectedTransactionWaitingPayback = new PaymentTransactionStatus(7);
                return rejectedTransactionWaitingPayback;
            }
        }

        public static PaymentTransactionStatus CompleteTransaction {
            get {
                if (completeTransaction == null)
                    completeTransaction = new PaymentTransactionStatus(99);
                return completeTransaction;
            }
        }

        public static PaymentTransactionStatus InvalidStatus {
            get {
                if (invalidStatus == null)
                    invalidStatus = new PaymentTransactionStatus(888);
                return invalidStatus;
            }
        }

        #endregion

        #region Equality

        public override bool Equals(Object obj) {
            PaymentTransactionStatus p = obj as PaymentTransactionStatus;
            if ((object)p == null) {
                return false;
            }

            return this.StatusId == p.StatusId;
        }

        public bool Equals(PaymentTransactionStatus p) {
            return this.StatusId == p.StatusId;
        }

        public override int GetHashCode() {
            return this.StatusId;
        }

        public static bool operator ==(PaymentTransactionStatus a, PaymentTransactionStatus b) {
            if (System.Object.ReferenceEquals(a, b))
                return true;

            if (((object)a == null) || ((object)b == null))
                return false;

            return a.StatusId == b.StatusId;
        }

        public static bool operator !=(PaymentTransactionStatus a, PaymentTransactionStatus b) {
            return !(a == b);
        }

        #endregion

    }
}